package cn.itsource.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.itsource.api.openfeign.ILoginApiToService;
import cn.itsource.constants.Constants;
import cn.itsource.constants.RConst;
import cn.itsource.pojo.domain.Customer;
import cn.itsource.mapper.CustomerMapper;
import cn.itsource.pojo.dto.LoginRo;
import cn.itsource.pojo.dto.RegisterDto;
import cn.itsource.pojo.ro.WxRegisterRo;
import cn.itsource.result.JSONResult;
import cn.itsource.result.PageResult;
import cn.itsource.service.ICustomerService;
import cn.itsource.service.WxService;
import cn.itsource.utils.BitStatesUtils;
import cn.itsource.utils.NameUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author zls
 * @since 2024-07-08
 */
@Service
@Transactional
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    private ILoginApiToService loginApiToService;
    @Autowired
    private WxService wxService;

    @Override
    @GlobalTransactional
    public JSONResult registerCustomer(WxRegisterRo registerRo) {
        //获取openId
        RegisterDto registerDto = wxService.getOpenId(registerRo.getCode());
        // 获取手机号
        //String phone = wxService.getPhone(wxRegisterRo.getPhoneCode());
        String phone ="19742322332";
        String name = NameUtil.getName();
        //判断当前乘客是否注册
        Customer one = super.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getOpenId, registerDto.getOpenid()));
        if (one==null){
            //初始化
            initCustomer(registerDto,phone,name);
            loginApiToService.saveLogin(buildLoginRo(registerDto,name));
            //测试seata 分布式事物
            //int i=1/0;
            return JSONResult.message(Constants.WxChat.SUCCESS_MESSAGE);
        }
        return JSONResult.message(Constants.WxChat.ALREADY_BOUND);
    }
    private static LoginRo buildLoginRo(RegisterDto registerDto,String name) {
        LoginRo loginRo = new LoginRo();
        loginRo.setPassword("123456");//todo
        loginRo.setType(RConst.Num.INT_2);
        loginRo.setOpenId(registerDto.getOpenid());
        loginRo.setUsername(name);
        loginRo.setAdmin(false);
        return loginRo;
    }


    private void initCustomer(RegisterDto registerDto,String phone,String name) {
        long l = BitStatesUtils.addState(BitStatesUtils.addState(RConst.Num.INT_0, BitStatesUtils.OP_PHONE), BitStatesUtils.OP_INIT_PAY_PASSWORD);
        //初始化乘客表
        Long snowflake = IdUtil.createSnowflake(RConst.Num.INT_1, RConst.Num.INT_1).nextId();
        Customer customer = new Customer()
                .setId(snowflake)
                .setPhone(phone)
                .setName(name)
                .setBitState(l)
                .setSex(RConst.Num.INT_0)
                .setOpenId(registerDto.getOpenid())
                .setCreateTime(new Date());
        super.save(customer);
    }
}
